<?php
class SafeFunc
{
	var $name;
	var $params;
	
	function SafeFunc($name, $params)
	{
		$this->name = $name;
		$this->params = $params;		
	}
	
	function Run()
	{
		if (!function_exists($this->name))
		{
    		echo "function $func not defined";
    		return;
		}
		$args = array();
		foreach ($this->params as $p)
		{
			if (!isset($_REQUEST[$p]))
			{
				echo "param " . $p . " not provided";
				return;
			}
			$args[$p] = mysql_real_escape_string($_REQUEST[$p]);
		}
		call_user_func($this->name, (object)$args);
	}
	
};

if(!function_exists('json_encode'))
{
	include_once('JSON.php');
	$GLOBALS['JSON_OBJECT'] = new Services_JSON();
	function json_encode($value)
	{
		return $GLOBALS['JSON_OBJECT']->encode($value); 
	}
	
	function json_decode($value)
	{
		return $GLOBALS['JSON_OBJECT']->decode($value); 
	}
}

function run_function($functions)
{
	$func = "";
	if (isset($_REQUEST["func"]))
		$func = mysql_real_escape_string($_REQUEST["func"]);
	
	if (isset($functions[$func]))
	{
		$functions[$func]->Run();
	}
	else
	{
	    echo "function $func not found";
	    return false;
	}
	return true;
}
	
?>